/**
 *  Copyright (C) 2008-2013  Telosys project org. ( http://www.telosys.org/ )
 *
 *  Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *          http://www.gnu.org/licenses/lgpl.html
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.telosys.persistence.impl;

import org.telosys.persistence.PersistenceManager;
import org.telosys.persistence.PersistenceManagerFactory;
import org.telosys.persistence.PersistenceTransaction;
import org.telosys.persistence.TelosysPersistence;
import org.telosys.persistence.TelosysPersistenceException;

public final class TransactionManager {

	private static PersistenceManagerFactory getPersistenceManagerFactory(int databaseId) {
		if ( databaseId >= 0 ) {
			return TelosysPersistence.getPersistenceManagerFactory(databaseId) ;
		}
		else {
			return TelosysPersistence.getPersistenceManagerFactory() ;
		}
	}
	
	private static PersistenceManagerImpl createNewPersistenceManager(int databaseId) {
		
		PersistenceManagerFactory persistenceManagerFactory = getPersistenceManagerFactory(databaseId) ;

		PersistenceManager pm = persistenceManagerFactory.createPersistenceManager() ;
		if ( pm instanceof PersistenceManagerImpl ) {
			return (PersistenceManagerImpl) pm ;
		}
		else {
			throw new IllegalStateException("PersistenceManager created is not an instance of 'PersistenceManagerImpl'");
		}
	}
	
    public static final boolean beginTransaction(int databaseId) 
    {
    	// Is there a current PM in this thread ?
    	PersistenceManagerImpl pm = CurrentPersistenceManager.get(); 
    	
    	if ( null == pm ) {
    		pm = createNewPersistenceManager(databaseId);
    		CurrentPersistenceManager.set(pm);
    	}
    	
    	PersistenceTransaction transaction = pm.getTransaction();
    	if ( transaction.isActive() == false ) {
    		transaction.begin();
    		return true ; // Transaction started 
    	}
    	else {
    		return false ; // Transaction not started here
    	}
    }

    public static final void rollbackTransaction() 
    {
    	PersistenceManagerImpl pm = CurrentPersistenceManager.get(); 
    	if ( pm != null ) {
        	PersistenceTransaction transaction = pm.getTransaction();
       		transaction.rollback();
    	}
    	else {
    		throw new TelosysPersistenceException("No current PersistenceManager");
    	}
    }

    public static final void commitTransaction() 
    {
    	PersistenceManagerImpl pm = CurrentPersistenceManager.get(); 
    	if ( pm != null ) {
        	PersistenceTransaction transaction = pm.getTransaction();
       		transaction.commit();
    	}
    	else {
    		throw new TelosysPersistenceException("No current PersistenceManager");
    	}
    }

    public static final void closePersistanceManager() 
    {
    	PersistenceManagerImpl pm = CurrentPersistenceManager.get(); 
    	if ( pm != null ) {
        	pm.close();
        	CurrentPersistenceManager.remove(pm);
    	}
    }
}
